package com.atguigu.day07

import org.apache.spark.sql.SparkSession

object $06_UDAF {

  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().master("local[4]").appName("test").getOrCreate()

    import spark.implicits._

    //注册udaf函数
    //弱类型udaf
    spark.udf.register("myMvg", new MyAvgLow)
    //
    import org.apache.spark.sql.functions._
    spark.udf.register("myAvg",udaf(new MyAvgHight))
    val df = List(
      ("lisi",20,"beijing"),
      ("wangwu",30,"shanghai")
      ,("zhaoliu",25,"beijing"),
      ("hanmeimei",32,"beijing")
      ,("lilei",50,"shanghai") ).toDF("name","age","address")
    df.rdd.mapPartitionsWithIndex((index,it)=>{
      println(s"index=${index} data=${it.toList}")
      it
    }).collect()
    //需求: 统计每个地区的年龄的平均值
    df.createOrReplaceTempView("person")
    spark.sql("select address,avg(age)  from person group by address").show
    spark.sql("select address,myMvg(age)  from person group by address").show
    spark.sql("select address,myAvg(age)  from person group by address").show
  }
}
